package sdk.org.apache.shiro.G1_工具类.C01_创建SecurityManager;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

import com.titan.core.utils.debug.Timer;
import com.titan.core.utils.io.ConsoleUtils;

import sdk.org.apache.shiro.utils.ShiroUtils;

public class 使用Ini文件实现SecurityManager {
    public static void main(String[] args) {
        Timer timer = Timer.$();
        try {
            run(timer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            timer.print("执行总用时");
            System.exit(0);
        }
    }
    private static void run(Timer timer) throws Exception {
        //1、获取SecurityManager工厂，此处使用Ini配置文件初始化SecurityManager
        Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory("classpath:sdk/org/apache/shiro/G1_工具类/C01_创建SecurityManager/shiro.ini");
        
        //2、得到SecurityManager实例 并绑定给SecurityUtils
        org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
        
        // 登录
        ShiroUtils.login("aaa", "111", true);
        
        // 断言用户已经登录
        Subject currentUser = SecurityUtils.getSubject();
        ConsoleUtils.log("用户 ["+currentUser.getPrincipal()+"] 的登录状态："+currentUser.isAuthenticated());
        
        // 用户退出
        ShiroUtils.logout();
    }
}
